<?php

class Backend_ItemController extends Backend_Libraries_Controller_Action
{

    public function indexAction() {

        // Get all Items
        $items = $this->_em->getRepository('Entities\Item')->findAll();

        // Assign to view
        $this->view->items = $items;
    }

    public function additemAction() {

        // Get Entity
        $objItem = new Entities\Item();
        // Get Validator class
        $objValidate = new Validator();
        // Get common class
        $objCommon = new Common();
        // Define variable
        $errorMes = array();
        $temp = false;
        $storeValues = array();

        // Use Message component
        $flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $errorMes = $flashMessenger->getMessages();

        // Get all categories
        $category = $this->_em->getRepository('Entities\Category')->findAll();
        // Get request was Posted
        $parrams = $this->getRequest();
        if ($parrams->isPost()) {
            $arr = $parrams->getParams();

            // Validate Name
            if (isset($arr['name']) && $objValidate->isAlphabetNumeric($arr['name'])) {
                $objItem->setName($arr['name']);
            } else {
                $flashMessenger->addMessage('Name not true.');
                $temp = true;
            }

            // Validate Code
            if (isset($arr['code']) && $objValidate->isAlphabetNumeric($arr['code']) && $objValidate->isLength($arr['code'], 4)) {
                $objItem->setCode($arr['code']);
            } else {
                $flashMessenger->addMessage('Code not true.');
                $temp = true;
            }

            // Validate Category
            if (isset($arr['category_id']) && is_numeric($arr['category_id'])) {
                $objCategory =$this->_em->getRepository('Entities\Category')->find(2);
                $objItem->setCategory($objCategory);
            } else {
                $flashMessenger->addMessage('Category not choose.');
                $temp = true;
            }

            // Validate Price
            if (isset($arr['price']) && $objValidate->isNumber($arr['price']) && !$objValidate->isNull($arr['price'])) {
                $objItem->setPrice($arr['price']);
            } else {
                $flashMessenger->addMessage('Price not true.');
                $temp = true;
            }

            // Validate Number
            if (isset($arr['number']) && is_numeric($arr['number'])) {
                $objItem->setNumber($arr['number']);
            } else {
                $flashMessenger->addMessage('Number not true.');
                $temp = true;
            }

            // Validate Status
            if (isset($arr['status']) && $objValidate->isInteger($arr['status'])) {
                $objItem->setStatus($arr['status']);
            } else {
                $flashMessenger->addMessage('Status not choose.');
                $temp = true;
            }

            // Insert DB
            if (!$temp) {
                $this->_em->persist($objItem);
                $this->_em->flush();
                $flashMessenger->addMessage('success');
            } else {
                $objCommon->setCookieCommon('StoreValues', $arr);
            }

            // Redirect
            $urlOptions = array('module' => 'backend', 'controller' => 'item', 'action' => 'additem');
            $this->_helper->redirector->gotoRoute($urlOptions, 'default', true);
            exit();
        }

        // Get cookie
        $storeValues = $this->_request->getCookie('StoreValues');
        $objCommon->removeCookieCommon('StoreValues');
        // Assign to view
        $this->view->arrStore = json_decode($storeValues);
        $this->view->messages = $errorMes;
        $this->view->category = $category;
    }
}